﻿using UnityEngine;
using System.Collections;

public class Bird {
	//GameObject[] bird = new GameObject[100];
	int soga=0;
	int sovit=0;
	int songan=0;
	public int vBullet=17000; //Van toc trung
	public int n = 0;
	int delaytime=180;
	Rigidbody rigid;
	public class bird {
		public GameObject _bird;
		public int direction;
		public int MoveSpeed;
		public int TimeMove;
	}

	public static ArrayList BirdStorage = new ArrayList();

	public void Load(){
//		bird[0] = Graphic.Draw ("ga1", new Vector2 (82, 88), "IMG/ga");
//		bird [0].transform.position = new Vector3 (0, 360, 3);
//		bird[1] = Graphic.Draw ("vit1", new Vector2 (82, 88), "IMG/vit");
//		bird [1].transform.position = new Vector3 (200, 360, 3);
//		bird[2] = Graphic.Draw ("ngan1", new Vector2 (82, 88), "IMG/ngan");
//		bird [2].transform.position = new Vector3 (-200, 360, 3);
		AddGa ();
		AddVit ();
		AddNgan ();
	}

	public void ResetGame()
	{
		for(int i=n-1;i>=0;i--)
		{
				bird a = (bird)BirdStorage [i];
				GameObject.Destroy (a._bird);
				BirdStorage.Remove (a);
				n--;
		}
		vBullet = 17000;
		songan = 0;
		sovit = 0;
		soga = 0;
		delaytime=180;
		AddGa ();
		AddVit ();
		AddNgan ();
	}


	public void SetVanToc()
	{
		if(Score.diem == 20)
		{
			vBullet = 20000;
			delaytime=100;
		}
		if(Score.diem == 40)
		{
			vBullet = 30000;
			delaytime=80;

		}
		if(Score.diem == 60)
		{
			vBullet = 40000;
			delaytime=50;

		}
	}

	public void GetInput()
	{
		if (Input.GetKeyDown(KeyCode.G))
			AddGa();
		if (Input.GetKeyDown(KeyCode.N))
			AddNgan();
		if (Input.GetKeyDown(KeyCode.V))
			AddVit();
	}

//	void Remove()
//	{
//		bird a = (bird)BirdStorage [1];
//		GameObject.Destroy (a._bird);
//		BirdStorage.Remove (a);
//		n--;
//
//
//	}

	public void AddGa()
	{
		soga++;
		bird ga = new bird ();
		ga._bird = Graphic.Draw ("ga"+soga, new Vector2 (82, 88), "IMG/ga");
		ga._bird.transform.position = new Vector3 (Random.Range(-580,580), 360, 3);
		ga.direction = Random.Range (0, 2);
		ga.MoveSpeed = Random.Range (120, 250);
		ga.TimeMove = 0;
		BirdStorage.Add (ga);
		n++;
		//Debug.Log (ga.MoveSpeed);
	}

	public void AddVit()
	{
		sovit++;
		bird vit = new bird ();
		vit._bird = Graphic.Draw ("vit"+sovit, new Vector2 (82, 88), "IMG/vit");
		vit._bird.transform.position = new Vector3 (Random.Range(-580,580), 360, 3);
		vit.direction = Random.Range (0, 2);
		vit.MoveSpeed = Random.Range (120, 250);
		vit.TimeMove = 0;
		BirdStorage.Add (vit);
		n++;
		//Debug.Log (vit.MoveSpeed);

	}

	public void AddNgan()
	{
		songan++;
		bird ngan = new bird ();
		ngan._bird = Graphic.Draw ("ngan"+songan, new Vector2 (82, 88), "IMG/ngan");
		ngan._bird.transform.position = new Vector3 (Random.Range(-580,580), 360, 3);
		ngan.direction = Random.Range (0, 2);
		ngan.MoveSpeed = Random.Range (120, 250);
		ngan.TimeMove = 0;
		BirdStorage.Add (ngan);
		n++;
		//Debug.Log (ngan.MoveSpeed);
	}

	int rand;
	int timer = 0;
	//float TimeMove=0;
	bird tmp;
	public void Update()
	{
		for(int i =0;i<n;i++)
		{
			tmp = (bird)BirdStorage[i];
			//Move(tmp._bird,tmp.direction,tmp.MoveSpeed);
			Move(tmp);
		}

		timer ++;
		if(timer>delaytime)
		{
			tmp = (bird)BirdStorage[Random.Range(0,n)];
			rand=Random.Range(0,3);
			if(rand==1 || rand==2)
			{
				GameObject G = new GameObject ("egg");
				G.transform.position = new Vector3(tmp._bird.transform.position.x,tmp._bird.transform.position.y-100,3);
				MeshFilter MF = G.AddComponent<MeshFilter> () as MeshFilter;
				MF.mesh = Graphic.DrawPlane (new Vector2(50,60), new Vector2(0,0), new Vector2 (1, 1));
				G.AddComponent <MeshRenderer> ();
				Material M = new Material (Resources.Load("Shaders/Alpha") as Shader);
				M.mainTexture = Resources.Load ("IMG/egg") as Texture;
				G.renderer.material = M;
				G.renderer.material.mainTextureScale = new Vector2(1,1);
				rigid = G.AddComponent<Rigidbody>();
				rigid.useGravity = false;


				rigid.AddForce(-tmp._bird.transform.up *vBullet);
				G.AddComponent <Bullet>();
				G.tag = "egg";
				SphereCollider S = G.AddComponent<SphereCollider> () as SphereCollider;
				S.radius = 30;
				S.isTrigger= true;
				timer=0;
			}
			if(rand==0)
			{
				GameObject G = new GameObject ("shit");
				G.transform.position = new Vector3(tmp._bird.transform.position.x,tmp._bird.transform.position.y-100,3);
				MeshFilter MF = G.AddComponent<MeshFilter> () as MeshFilter;
				MF.mesh = Graphic.DrawPlane (new Vector2(60,60), new Vector2(0,0), new Vector2 (1, 1));
				G.AddComponent <MeshRenderer> ();
				Material M = new Material (Resources.Load("Shaders/Alpha") as Shader);
				M.mainTexture = Resources.Load ("IMG/shit") as Texture;
				G.renderer.material = M;
				G.renderer.material.mainTextureScale = new Vector2(1,1);
				rigid = G.AddComponent<Rigidbody>();
				rigid.useGravity = false;
				
				
				rigid.AddForce(-tmp._bird.transform.up *vBullet);
				G.AddComponent <Bullet>();
				G.tag = "shit";
				SphereCollider S = G.AddComponent<SphereCollider> () as SphereCollider;
				S.radius = 30;
				S.isTrigger= true;
				timer=0;
			}

		}

	}

	void Move(bird tmp)
	{
		if (tmp.direction == 1) 
		{
			if (tmp.TimeMove < tmp.MoveSpeed && tmp._bird.transform.position.x<=600 ) 
			{
				tmp._bird.transform.position += new Vector3 (3, 0, 0);
				if(tmp._bird.transform.position.x==600)
					tmp.TimeMove= tmp.MoveSpeed;
				tmp.TimeMove ++;
			} else if (tmp.TimeMove < tmp.MoveSpeed * 2 && tmp._bird.transform.position.x >=-600) 
			{
				tmp._bird.transform.position -= new Vector3 (3, 0, 0);
				if(tmp._bird.transform.position.x==-600)
					tmp.TimeMove= tmp.MoveSpeed*3;
				tmp.TimeMove ++;
			} else if (tmp.TimeMove < tmp.MoveSpeed * 3 && tmp._bird.transform.position.x >=-600) 
			{
				tmp._bird.transform.position -= new Vector3 (3, 0, 0);
				if(tmp._bird.transform.position.x==-600)
					tmp.TimeMove= tmp.MoveSpeed*3;
				tmp.TimeMove ++;
			} else if (tmp.TimeMove < tmp.MoveSpeed * 4 && tmp._bird.transform.position.x <=600) 
			{
				tmp._bird.transform.position += new Vector3 (3, 0, 0);
				if(tmp._bird.transform.position.x==600)
					tmp.TimeMove=tmp.MoveSpeed;
				tmp.TimeMove ++;
				if (tmp.TimeMove == tmp.MoveSpeed * 4) 
				{
					tmp.TimeMove = 0;
				}
						
			}
		}
		if (tmp.direction == 0) 
		{
			if (tmp.TimeMove < tmp.MoveSpeed && tmp._bird.transform.position.x>=-600 ) 
			{
				tmp._bird.transform.position -= new Vector3 (3, 0, 0);
				if(tmp._bird.transform.position.x==-600)
					tmp.TimeMove= tmp.MoveSpeed;
				tmp.TimeMove ++;
			} else if (tmp.TimeMove < tmp.MoveSpeed * 2 && tmp._bird.transform.position.x <=600) 
			{
				tmp._bird.transform.position += new Vector3 (3, 0, 0);
				if(tmp._bird.transform.position.x==600)
					tmp.TimeMove= tmp.MoveSpeed*3;
				tmp.TimeMove ++;
			} else if (tmp.TimeMove < tmp.MoveSpeed * 3 && tmp._bird.transform.position.x <=600) 
			{
				tmp._bird.transform.position += new Vector3 (3, 0, 0);
				if(tmp._bird.transform.position.x==600)
					tmp.TimeMove= tmp.MoveSpeed*3;
				tmp.TimeMove ++;
			} else if (tmp.TimeMove < tmp.MoveSpeed * 4 && tmp._bird.transform.position.x >=-600) 
			{
				tmp._bird.transform.position -= new Vector3 (3, 0, 0);
				if(tmp._bird.transform.position.x==-600)
					tmp.TimeMove=tmp.MoveSpeed;
				tmp.TimeMove ++;
				if (tmp.TimeMove == tmp.MoveSpeed * 4) 
				{
					tmp.TimeMove = 0;
				}
				
			}
		}
	}

//	void Move(GameObject a,int b,int MoveSpeed)
//	{
//
//		if (b == 1) 
//		{
//			if (TimeMove < MoveSpeed && a.transform.position.x<=600 ) 
//			{
//				a.transform.position += new Vector3 (3, 0, 0);
//				if(a.transform.position.x==600)
//					TimeMove= MoveSpeed;
//				TimeMove ++;
//			} else if (TimeMove < MoveSpeed * 2 && a.transform.position.x >=-600) 
//			{
//				a.transform.position -= new Vector3 (3, 0, 0);
//				if(a.transform.position.x==-600)
//					TimeMove= MoveSpeed*3;
//				TimeMove ++;
//			} else if (TimeMove < MoveSpeed * 3 && a.transform.position.x >=-600) 
//			{
//				a.transform.position -= new Vector3 (3, 0, 0);
//				if(a.transform.position.x==-600)
//					TimeMove= MoveSpeed*3;
//				TimeMove ++;
//			} else if (TimeMove < MoveSpeed * 4 && a.transform.position.x <=600) 
//			{
//				a.transform.position += new Vector3 (3, 0, 0);
//				if(a.transform.position.x==600)
//					TimeMove=MoveSpeed;
//				TimeMove ++;
//				if (TimeMove == MoveSpeed * 4) 
//				{
//					TimeMove = 0;
//				}
//						
//			}
//		}
//		if (b == 0) 
//		{
//			if (TimeMove < MoveSpeed && a.transform.position.x>=-600 ) 
//			{
//				a.transform.position -= new Vector3 (3, 0, 0);
//				if(a.transform.position.x==-600)
//					TimeMove= MoveSpeed;
//				TimeMove ++;
//			} else if (TimeMove < MoveSpeed * 2 && a.transform.position.x <=600) 
//			{
//				a.transform.position += new Vector3 (3, 0, 0);
//				if(a.transform.position.x==600)
//					TimeMove= MoveSpeed*3;
//				TimeMove ++;
//			} else if (TimeMove < MoveSpeed * 3 && a.transform.position.x <=600) 
//			{
//				a.transform.position += new Vector3 (3, 0, 0);
//				if(a.transform.position.x==600)
//					TimeMove= MoveSpeed*3;
//				TimeMove ++;
//			} else if (TimeMove < MoveSpeed * 4 && a.transform.position.x >=-600) 
//			{
//				a.transform.position -= new Vector3 (3, 0, 0);
//				if(a.transform.position.x==-600)
//					TimeMove=MoveSpeed;
//				TimeMove ++;
//				if (TimeMove == MoveSpeed * 4) 
//				{
//					TimeMove = 0;
//				}
//				
//			}
//		}
//	}


}
